﻿using System;
using System.ComponentModel;
using System.Globalization;

namespace HML
{
    /// <summary>
    ///MultidropSlide滑杆停靠外边距
    /// </summary>
    [Serializable]
    [TypeConverterAttribute(typeof(MultidropSlidePoleAnchorMarginConverter))]
    public class MultidropSlidePoleAnchorMargin
    {
        #region 字段

        private bool _all;
        private int _poleTitle;
        private int _poleStart;
        private int _poleEnd;

        public static readonly MultidropSlidePoleAnchorMargin Empty = new MultidropSlidePoleAnchorMargin(0);

        #endregion

        #region 属性

        /// <summary>
        /// 全部
        /// </summary>
        [Description("全部")]
        [DefaultValue(0)]
        [RefreshProperties(RefreshProperties.All)]
        public int All
        {
            get
            {
                return this._all ? this._poleTitle : -1;
            }
            set
            {
                if (value < 0)
                {
                    return;
                }

                if (this._all != true || this._poleTitle != value)
                {
                    this._all = true;
                    this._poleTitle = this._poleStart = this._poleEnd = value;
                }
            }
        }

        /// <summary>
        /// 滑杆为提示标题保留外边距
        /// </summary>
        [Description("滑杆为提示标题保留外边距")]
        [DefaultValue(0)]
        [RefreshProperties(RefreshProperties.All)]
        public int PoleTitle
        {
            get
            {
                if (this._all)
                {
                    return this._poleTitle;
                }
                return this._poleTitle;
            }
            set
            {
                if (value < 0)
                {
                    value = 0;
                }

                if (this._all || this._poleTitle != value)
                {
                    this._all = false;
                    this._poleTitle = value;
                }
            }
        }

        /// <summary>
        /// 滑杆开始坐标外边距
        /// </summary>
        [Description("滑杆开始坐标外边距")]
        [DefaultValue(0)]
        [RefreshProperties(RefreshProperties.All)]
        public int PoleStart
        {
            get
            {
                if (this._all)
                {
                    return this._poleTitle;
                }
                return this._poleStart;
            }
            set
            {
                if (value < 0)
                {
                    value = 0;
                }

                if (this._all || this._poleStart != value)
                {
                    this._all = false;
                    this._poleStart = value;
                }
            }
        }

        /// <summary>
        /// 滑杆结束坐标外边距
        /// </summary>
        [Description("滑杆结束坐标外边距")]
        [DefaultValue(0)]
        [RefreshProperties(RefreshProperties.All)]
        public int PoleEnd
        {
            get
            {
                if (this._all)
                {
                    return this._poleTitle;
                }
                return this._poleEnd;
            }
            set
            {
                if (value < 0)
                {
                    value = 0;
                }

                if (this._all || this._poleEnd != value)
                {
                    this._all = false;
                    this._poleEnd = value;
                }
            }
        }

        #endregion

        #region  构造

        /// <summary>
        /// 
        /// </summary>
        /// <param name="all">全部</param>
        public MultidropSlidePoleAnchorMargin(int all)
        {
            this._all = true;
            this._poleTitle = this._poleStart = this._poleEnd = all;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="poleTitle">滑杆为提示标题保留外边距</param>
        /// <param name="poleStart">滑杆开始坐标外边距</param>
        /// <param name="poleEnd">滑杆结束坐标外边距</param>
        public MultidropSlidePoleAnchorMargin(int poleTitle, int poleStart, int poleEnd)
        {
            this._poleTitle = poleTitle;
            this._poleStart = poleStart;
            this._poleEnd = poleEnd;
            this._all = (this._poleTitle == this._poleStart && this._poleTitle == this._poleEnd);
        }

        #endregion

        #region 运算符重载

        /// <summary>
        /// 判断两个滑杆停靠外边距对象设置是否一样
        /// </summary>
        /// <param name="arg1"></param>
        /// <param name="arg2"></param>
        /// <returns></returns>
        public static bool operator ==(MultidropSlidePoleAnchorMargin arg1, MultidropSlidePoleAnchorMargin arg2)
        {
            return arg1.PoleTitle == arg2.PoleTitle && arg1.PoleStart == arg2.PoleStart && arg1.PoleEnd == arg2.PoleEnd;
        }

        /// <summary>
        /// 判断两个滑杆停靠外边距对象设置是否不一样
        /// </summary>
        /// <param name="arg1"></param>
        /// <param name="arg2"></param>
        /// <returns></returns>
        public static bool operator !=(MultidropSlidePoleAnchorMargin arg1, MultidropSlidePoleAnchorMargin arg2)
        {
            return !(arg1 == arg2);
        }

        #endregion

        #region 重写

        /// <summary>
        /// 获取该对象的哈希值（参考源码）
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return this.PoleTitle ^ RotateLeftTop(this.PoleEnd, 8) ^ RotateLeftTop(this.PoleStart, 16);
        }

        /// <summary>
        /// 判断是否和该对象的引用是一样
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public override bool Equals(object other)
        {
            if (other is MultidropSlidePoleAnchorMargin)
            {
                return ((MultidropSlidePoleAnchorMargin)other) == this;
            }
            return false;
        }

        /// <summary>
        /// 把该对象转换成字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return "{PoleTitle = " + this.PoleTitle.ToString(CultureInfo.CurrentCulture) + ",PoleEnd = " + this.PoleEnd.ToString(CultureInfo.CurrentCulture) + ",PoleStart = " + this.PoleStart.ToString(CultureInfo.CurrentCulture) + "}";
        }

        #endregion

        #region 方法

        /// <summary>
        /// 计算哈希值（参考源码）
        /// </summary>
        /// <param name="value"></param>
        /// <param name="nBits"></param>
        /// <returns></returns>
        public static int RotateLeftTop(int value, int nBits)
        {
            nBits = nBits % 32;
            return value << nBits | (value >> (32 - nBits));
        }

        /// <summary>
        /// 是否应该使用All设置初始化对象
        /// </summary>
        /// <returns></returns>
        public bool ShouldSerializeAll()
        {
            return this._all;
        }

        #endregion
    }

}
